package com.yunsc.order.sevice.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.order.dao.YunscPaymentDao;
import com.yunsc.order.entity.YunscPaymentEntity;
import com.yunsc.order.entity.YunscRechargePlanEntity;
import com.yunsc.order.sevice.YunscPaymentService;
import com.yunsc.order.sevice.YunscRechargePlanService;
import com.yunsc.order.vo.PersonalVO;
import com.yunsc.order.vo.RechargeVO;
import com.yunsc.store.entity.YunscStoreSettingEntity;
import com.yunsc.store.entity.YunscUserEntity;
import com.yunsc.store.service.YunscStoreSettingService;
import com.yunsc.store.service.YunscUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service("yunscPaymentService")
public class YunscPaymentServiceImpl extends ServiceImpl<YunscPaymentDao, YunscPaymentEntity> implements YunscPaymentService {

    @Resource
    private YunscStoreSettingService storeSettingService;
    @Resource
    private YunscUserService userService;
    @Resource
    private YunscRechargePlanService rechargePlanService;


    @Override
    public List<YunscPaymentEntity> queryPaymentMethodsByClient(String client, Integer storeId) {
        LambdaQueryWrapper<YunscPaymentEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YunscPaymentEntity::getClient, client).eq(YunscPaymentEntity::getStoreId, storeId);
        lqw.orderByAsc(YunscPaymentEntity::getPaymentId);
        return this.list(lqw);
    }

    @Override
    public RechargeVO queryRechargeCenter(String client, Integer userId, Integer storeId) {

        List<YunscPaymentEntity> paymentList = this.queryPaymentMethodsByClient(client, storeId);
        paymentList.removeIf(payment -> "balance".equals(payment.getMethod()));
        YunscStoreSettingEntity storeSetting = storeSettingService.queryStoreSettingByKey("recharge", storeId);
        if (storeSetting == null) {
            return null;
        }
        YunscUserEntity user = userService.queryUserInfo(userId);
        PersonalVO personalVO = new PersonalVO();
        BeanUtils.copyProperties(user, personalVO);

        List<YunscRechargePlanEntity> planList = rechargePlanService.list(new LambdaQueryWrapper<YunscRechargePlanEntity>()
                .eq(YunscRechargePlanEntity::getStoreId, storeId));


        RechargeVO rechargeVO = new RechargeVO();
        rechargeVO.setPaymentMethods(paymentList);
        rechargeVO.setPersonal(personalVO);
        rechargeVO.setSetting(JSON.parseObject(storeSetting.getValues()));
        rechargeVO.setPlanList(planList);
        return rechargeVO;
    }
}